package com.mine.frame.mylandemo.ui.study

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.util.Log
import com.blankj.utilcode.util.SnackbarUtils
import com.mine.frame.common.base.BaseActivity
import com.mine.frame.common.empty.EmptyPresenter
import com.mine.frame.common.empty.IEmpty
import com.mine.frame.mylandemo.R
import kotlinx.android.synthetic.main.atv_distance.*

/**
 * @Date 2022/8/10
 * Description:距离感应器
 */
class DistanceAtv : BaseActivity<IEmpty, EmptyPresenter>(), IEmpty , SensorEventListener {
    private var sensorManager :SensorManager ? = null
    private var pSensor: Sensor ? = null
    companion object{
        const val TAG = "MySensor==>>"
    }
    override fun initLayoutId(): Int = R.layout.atv_distance

    override fun initPresenter(): EmptyPresenter = EmptyPresenter(this)

    override fun initLayoutAfter(savedInstanceState: Bundle?) {

    }

    override fun onResume() {
        super.onResume()
        openSensorDistance()
    }


    /**
     * 开启距离传感器
     */
    private fun openSensorDistance(){
        pSensor?.let {
            addSensorListener()
        }?:let {
            sensorManager?.let {
                pSensor = it.getDefaultSensor(Sensor.TYPE_PROXIMITY)
            }?:let {
                sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
                pSensor = sensorManager?.getDefaultSensor(Sensor.TYPE_PROXIMITY)
            }

            if (null == pSensor){
                SnackbarUtils.with(tvHint).setMessage("当前机器不存在距离感应器").showError(true)
            }else{
                SnackbarUtils.with(tvHint).setMessage("距离感应器已开启").showSuccess()
                addSensorListener()
            }
        }
    }

    /**
     * 添加距离感应器监听
     */
    private fun addSensorListener(){
        sensorManager?.let { manager ->
            pSensor?.let {sensor->
                manager.registerListener(this ,sensor , SensorManager.SENSOR_DELAY_GAME)
            }
        }
    }

    /**
     * 注销监听
     */
    private fun unRegisterSensorListener(){
        sensorManager?.let { manager ->
            pSensor?.let {sensor->
                manager.unregisterListener(this ,sensor)
            }
        }
    }

    override fun onSensorChanged(event: SensorEvent?) {
        event?.let {
            if (it.sensor.type == Sensor.TYPE_PROXIMITY){
                val distance = it.values[0]
                if (distance == 0f){
                    tvHint.setBackgroundResource(R.color.black)
                }else{
                    tvHint.setBackgroundResource(R.color.white)
                }
                Log.e(TAG,"距离:${distance}")
            }
        }
    }

    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
        Log.e(TAG ,"onAccuracyChanged:"  + "\n sensor.type:${sensor?.type}"+ "\n accuracy:${accuracy}" + "\n MaximumRange:${sensor?.maximumRange}")
    }

    override fun onDestroy() {
        super.onDestroy()
        unRegisterSensorListener()
    }
}